package com.bida.controller.user;


import com.bida.constant.AddressBookConstant;
import com.bida.domain.po.AddressBook;
import com.bida.result.Result;
import com.bida.service.IAddressBookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 地址簿 前端控制器
 * </p>
 *
 * @author ly
 * @since 2024-09-08
 */
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Resource
    private IAddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    /**
     *查询当前登录用户的所有地址信息
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> getAddresssBookList(){
        List<AddressBook> list = addressBookService.getAddresssBookList();
        return Result.success(list);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddressInfo(){
        AddressBook addressBook = addressBookService.getDefaultAddressInfo();
        return Result.success(addressBook);
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result updateAddressBookById(@RequestBody AddressBook addressBook){
        addressBook.setIsDefault(AddressBookConstant.NOT_DEFAULT_ADDRESS);
        addressBookService.updateById(addressBook);
        return Result.success();
    }


    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteAddressBook(Long id){
        addressBookService.removeById(id);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBookInfoById(@PathVariable("id") Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setDefaultAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.setDefaultAddressBook(addressBook);
        return Result.success();
    }


}
